﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 左上部分 选择人物
    /// </summary>
    public class SkillEffectControlWindow : SkillSecondWindow
    {

        private string[] commondTexts = { "选择svn操作..", "更新", "提交", "还原", "cleanup", "log" };
        private string rolePrefabsPath = "Assets/Res/Character/Role/Prefab";
        private string bossPrefabPath = "Assets/Res/Character/Boss/Prefab";
        private string petPrefabPath = "Assets/Res/Character/Pet/Prefab";

        private List<string> rolePrefabNames = new List<string>();
        private List<string> bossPrefabNames = new List<string>();
        private List<string> petPrefabNames = new List<string>();

        static public int currentSelectPage;
        private int currentSelectIndex;
        private SkillEffectInfo selectInfo = null;

        public SkillEffectControlWindow(EffectEditor t, float x, float y, float width, float height) : base(t, x, y, width, height)
        {

        }

        protected override void Draw(int id)
        {
            int command = 0;
            EditorGUI.BeginChangeCheck();
            command = EditorGUILayout.Popup(command, commondTexts);
            if (EditorGUI.EndChangeCheck())
            {
                string path1 = Application.dataPath + @"/Res/EffectConfig/";
                string path2 = Application.dataPath + @"/Res/Effect/Prefab";
                // string path = ""

                switch (command)
                {
                    case 1:
                        {
                            //SVN.svnUp(path1 + "*" + path2);
                            break;
                        }
                    case 2:
                        {
                            //SVN.svnCo(path1 + "*" + path2);
                            break;
                        }
                    case 3:
                        {
                            //SVN.svnRevert(path1 + "*" + path2);
                            break;
                        }
                    case 4:
                        {
                            //SVN.svnCleanup(path1 + "*" + path2);
                            break;
                        }
                    case 5:
                        {
                            //SVN.svnLog(path1 + "*" + path2);
                            break;
                        }
                }
            }

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("主角"))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(rolePrefabsPath);
                var fileInfos = dirInfo.GetFiles();

                rolePrefabNames.Clear();
                rolePrefabNames.Add("<no selected>");
                foreach (var fi in fileInfos)
                {
                    if (fi.Name.Contains("zhujue") && !fi.Name.Contains(".meta") && fi.Name.Contains("01"))
                    {
                        string name = fi.Name.Remove(fi.Name.Length - 7, 7);
                        rolePrefabNames.Add(name);
                    }
                }

                currentSelectPage = 1;
                currentSelectIndex = 0;
            }

            if (GUILayout.Button("Boss"))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(bossPrefabPath);
                var fileInfos = dirInfo.GetFiles();

                bossPrefabNames.Clear();
                bossPrefabNames.Add("<no selected>");
                foreach (var fi in fileInfos)
                {
                    if (fi.Name.Contains("boss") && !fi.Name.Contains(".meta"))
                    {
                        string name = fi.Name.Remove(fi.Name.Length - 7, 7);
                        bossPrefabNames.Add(name);
                    }
                }

                currentSelectPage = 2;
                currentSelectIndex = 0;
            }

            #region 宠物

            if (GUILayout.Button("宠物"))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(petPrefabPath);
                var fileInfos = dirInfo.GetFiles();

                petPrefabNames.Clear();
                petPrefabNames.Add("<no selected>");
                foreach (var fi in fileInfos)
                {
                    if (fi.Name.Contains("pet") && !fi.Name.Contains(".meta"))
                    {
                        string name = fi.Name.Remove(fi.Name.Length - 7, 7);
                        petPrefabNames.Add(name);
                    }
                }

                currentSelectPage = 3;
                currentSelectIndex = 0;
            }

            #endregion

            GUILayout.EndHorizontal();
            if (GUILayout.Button("刷新骨骼"))
            {
                if (root.createdObject != null)
                {
                    var bouns = root.createdObject.transform.GetComponentsInChildren<Transform>(true);
                    root.effectBones.Clear();
                    root.effectBones.Add("原点", null);

                    foreach (var name in root.bounsName)
                    {

                        foreach (var boun in bouns)
                        {
                            if (boun.name.ToLower().Contains(name.ToLower()))
                            {
                                root.effectBones.Add(boun.name, boun);
                            }
                        }
                    }
                }
            }

            EditorGUI.BeginChangeCheck();
            if (currentSelectPage == 1)
            {
                currentSelectIndex = EditorGUILayout.Popup(currentSelectIndex, rolePrefabNames.ToArray());
            }
            else if (currentSelectPage == 2)
            {
                currentSelectIndex = EditorGUILayout.Popup(currentSelectIndex, bossPrefabNames.ToArray());
            }
            else if (currentSelectPage == 3)
            {
                currentSelectIndex = EditorGUILayout.Popup(currentSelectIndex, petPrefabNames.ToArray());
            }

            if (EditorGUI.EndChangeCheck())
            {
                if (currentSelectPage == 1)
                {
                    string fullPath = rolePrefabsPath + "/" + rolePrefabNames[currentSelectIndex] + ".prefab";
                    root.obj = AssetDatabase.LoadAssetAtPath<GameObject>(fullPath);
                }
                else if (currentSelectPage == 2)
                {
                    string fullPath = bossPrefabPath + "/" + bossPrefabNames[currentSelectIndex] + ".prefab";
                    root.obj = AssetDatabase.LoadAssetAtPath<GameObject>(fullPath);
                }
                else if (currentSelectPage == 3)
                {
                    string fullPath = petPrefabPath + "/" + petPrefabNames[currentSelectIndex] + ".prefab";
                    root.obj = AssetDatabase.LoadAssetAtPath<GameObject>(fullPath);
                }


                if (root.obj != null)
                {
                    CreateNewObject();
                    root.meshPlayer = CreateAnimatorControl();

                    root.attackTarget.transform.position = root.createdObject.transform.forward * 5;
                    root.attackTarget.transform.LookAt(root.createdObject.transform);
                    SkillEffectEvent e = new SkillEffectEvent();
                    e.eventT = (int)SkillEffectEventEnum.ChangeObj;
                    root.OnEvent(e);
                }
            }

        }


        //根据
        public void DrawControl()
        {

        }



        public override void OnEditorEvent(IEditorEvent e)
        {
            switch ((SkillEffectEventEnum)e.GetEventID())
            {
                case SkillEffectEventEnum.SkillSelected:
                    /*string index = (string)e.GetEventData();
                    SkillEffectInfo info = null;
                    if (root.skillData.data.TryGetValue(index, out info))
                    {
                        selectInfo = info;
                    }
                    foreach (var player in root.m_effectPlayer)
                    {
                        player.Release();
                    }
                    root.m_effectPlayer.Clear();*/
                    break;
            }
        }



        void CreateNewObject()
        {
            /*if (root.m_createdObject != null)
                GameObject.DestroyImmediate(root.m_createdObject);
            if (root.m_obj != null)
            {
                root.m_createdObject = GameObject.Instantiate(m_root.m_obj) as GameObject;
                root.m_createdObject.transform.position = Vector3.zero;
                root.m_createdObject.transform.rotation = Quaternion.identity;
                
                List<Transform> bouns = Util.Helper.GetAllChildTransform(root.m_createdObject.transform);
                root.m_effectBones.Clear();
                root.m_effectBones.Add("原点", null);

                foreach (var name in root.m_bounsName)
                {

                    foreach (var boun in bouns)
                    {
                        if (boun.name.ToLower().Contains(name.ToLower()))
                        {
                            root.effectBones.Add(boun.name, boun);
                        }
                    }
                }
                
                foreach (var player in m_root.m_effectPlayer)
                {
                    player.Release();
                }
                m_root.m_effectPlayer.Clear();
            }*/
        }



        AnimatorEditorPlayer CreateAnimatorControl()
        {
            Animator animator = root.createdObject.GetComponent<Animator>();
            AnimatorEditorPlayer player = new AnimatorEditorPlayer(animator);
            return player;
        }
    }
}
